/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.skatiger.jrove.spring.form.renderer;

import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.component.UIComponent;
import org.skatiger.jrove.renderer.HtmlRendererBase;
import org.springframework.util.Assert;
import org.springframework.web.servlet.support.BindStatus;

public abstract class AbstractFormElementRenderer extends HtmlRendererBase {
    protected static final String DISPALY_STRING_PROPERTY_NAME = "displayString";
    protected static final String BIND_STATUS_PROPERTY_NAME = "bindStatus";
    protected static final String HTML_ESCAPE_PROPERTY_NAME = "htmlEscape";

    protected String getDisplayString(UIComponent component) {
        Object content = component.getAttributes().get(
                DISPALY_STRING_PROPERTY_NAME);
        if (content != null) {
            return content.toString();
        }
        return "";
    }

    protected boolean isHtmlEscape(UIComponent component) {
        Object content = component.getAttributes().get(
                HTML_ESCAPE_PROPERTY_NAME);
        return (Boolean) content;
    }

    protected BindStatus getBindStatus(UIComponent component) {
        Object bindStatus = component.getAttributes().get(
                BIND_STATUS_PROPERTY_NAME);
        Assert.notNull(bindStatus, "form element's BindStatus can not be null");
        Assert.isInstanceOf(BindStatus.class, bindStatus);
        return (BindStatus) bindStatus;
    }

    @Override
    protected boolean shouldRenderId(ViewContext context, UIComponent component) {
        return true;//already render id
    }
}
